package top.z.work.module.system.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import top.z.work.framework.common.exception.ServiceException;
import top.z.work.module.system.entity.SystemDictData;
import top.z.work.module.system.entity.SystemDictType;
import top.z.work.module.system.mapper.SystemDictTypeMapper;
import top.z.work.module.system.service.SystemDictDataService;
import top.z.work.module.system.service.SystemDictTypeService;

import static top.z.work.module.system.constant.SystemErrorCode.DICT_TYPE_ASSIGN_TO_DATA;
import static top.z.work.module.system.constant.SystemErrorCode.DICT_TYPE_ID_NOT_EMPTY;

/**
 * 字典类型表ServiceImpl
 *
 * @author zaoangod
 * @since 2024-03-22
 */
@Service
public class SystemDictTypeServiceImpl extends ServiceImpl<SystemDictTypeMapper, SystemDictType> implements SystemDictTypeService {

    @Resource
    SystemDictDataService systemDictDataService;

    /**
     * 删除字典类型
     *
     * @param identity 数据ID
     */
    @Override
    public void removeDictType(Long identity) {
        if (identity == null || identity == 0) {
            throw new ServiceException(DICT_TYPE_ID_NOT_EMPTY);
        }
        var queryData = QueryChain.of(SystemDictData.class).eq(SystemDictData::getParent, identity);
        if (systemDictDataService.exists(queryData)) {
            throw new ServiceException(DICT_TYPE_ASSIGN_TO_DATA);
        }
        this.removeById(identity);
    }
}
